package com.scisoft.scan.ws;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scisoft.scan.data.QueryPageBean;
import com.scisoft.scan.data.delivery.CustomerDeliveryBean;
import com.scisoft.scan.data.delivery.DeliveryBean;
import com.scisoft.scan.data.delivery.DeliveryOrderBean;
import com.scisoft.scan.data.delivery.DeliveryOrderDetailBean;
import com.scisoft.scan.data.delivery.DeliveryQueryPageBean;
import com.scisoft.scan.data.delivery.LogisticsCompanyBean;
import com.scisoft.scan.data.delivery.LogisticsInfo;
import com.scisoft.scan.data.delivery.LogisticsPictureUrlBean;
import com.scisoft.scan.data.stock.StockInfoBean;

import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.HeaderProperty;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.ArrayList;
import java.util.List;


public class WebServiceHelper_Delivery {

    public static String GetWS_Url() {
        return String.format("http://%s/dymps/ws/SciDeliveryServer?wsdl", SysParameter.WS_IPandPort);
    }

    public static String WS_Method = "receive";

    public static String WS_Namespace = "http://delivery.scan.ws.dms.com/";

    //查询发运单的主单信息 bizId 查询条件  flag 1 未发 2 发运中 3 已发
    public static DeliveryQueryPageBean DELIVERY_01(String bizId, String flag) throws Exception {

//        DeliveryQueryPageBean ret = new DeliveryQueryPageBean();
//        ret.pageCount = 10;
//        ret.pageSize = 10;
//        ret.totalCount = 100;
//        ret.inCount = 10;
//        ret.notCount = 11;
//        ret.okCount = 12;
//        return ret;

        if(flag==null || "".equals(flag)){
            flag="@@";
        }
        String biz = bizId + ","+ flag;
        String arg0 = String.format( "{\"flowNo\":\"DELIVERY_01\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,biz);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        DeliveryQueryPageBean ret = new DeliveryQueryPageBean();
        if (msg!=null&&msg.contains("成功"))
        {
            JSONObject jsonArray = data.getJSONObject("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<DeliveryQueryPageBean>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //查询发运单的订单信息 bizId 查询条件  flag 1 未发 2 发运中 3 已发
    public static List<DeliveryBean> DELIVERY_02(String bizId, String flag, int pagesize, int pageindex) throws Exception {
//        List<DeliveryBean> ret = new ArrayList<>();
//        DeliveryBean d = new DeliveryBean();
//        d.customer = "东风汽车厂";
//        d.telephone = "04361255895";
//        d.contact = "张三";
//        d.address = "吉林省长春市北湖109号";
//        d.orderNo = "N0293u4p2u323";
//        d.detail = "10项90件";
//        d.orderType = "出库";
//        d.depotName = "J仓库";
//        ret.add(d);
//        return ret;
        if(flag==null || "".equals(flag)){
            flag="@@";
        }
        String biz = bizId + ","+ flag;
        List<DeliveryBean> ret = new ArrayList<>();
        String arg0 = String.format( "{\"flowNo\":\"DELIVERY_02\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\",\"pageIndex\":\"%s\",\"pageSize\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,biz,pageindex,pagesize);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<List<DeliveryBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    /*
        根据主单的ID，查询主单下关联的订单列表
     */
    public static List<DeliveryOrderBean> DELIVERY_03(String deliveryId) throws Exception {
//        List<DeliveryOrderBean> ret = new ArrayList<>();
//        DeliveryOrderBean deliveryOrderBean = new DeliveryOrderBean();
//        deliveryOrderBean.orderNo = "PSLS0987654321";
//        deliveryOrderBean.orderType = "常规订单";
//        deliveryOrderBean.amount = "5210.5 元";
//        deliveryOrderBean.isFree = "是";
//        deliveryOrderBean.orderInfo = "2项10件";
//        ret.add(deliveryOrderBean);
//        return ret;
        List<DeliveryOrderBean> ret = new ArrayList<>();
        String arg0 = String.format( "{\"flowNo\":\"DELIVERY_03\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,deliveryId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<List<DeliveryOrderBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //根据主单的客户，查询同一个客户可以合并发运的订单列表
    public static List<DeliveryOrderBean> DELIVERY_04(String address,String customerId,String warehouserId,String shipId) throws Exception {
//        List<DeliveryOrderBean> ret = new ArrayList<>();
//        DeliveryOrderBean deliveryOrderBean = new DeliveryOrderBean();
//        deliveryOrderBean.orderNo = "1111111111";
//        deliveryOrderBean.orderType = "常规订单";
//        deliveryOrderBean.amount = "200 元";
//        deliveryOrderBean.isFree = "是";
//        deliveryOrderBean.orderInfo = "1项2件";
//        ret.add(deliveryOrderBean);
//        return ret;
        String bizId =address+","+ customerId + ","+ warehouserId+","+shipId;
        List<DeliveryOrderBean> ret = new ArrayList<>();
        String arg0 = String.format( "{\"flowNo\":\"DELIVERY_04\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<List<DeliveryOrderBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }


    //选择一个订单合并到发运单一起发运
    public static String DELIVERY_05(String deliveryId, String orderId, String orderNo) throws Exception {
        String bizId = deliveryId + "," + orderId + "," + orderNo;
        String arg0 = String.format("{\"flowNo\":\"DELIVERY_05\",\"scSequence\":\"%s\",\"account\":\"%s\",\"bizId\":\"%s\"}", SysParameter.ScSequence, SysParameter.user, bizId);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析jso
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        String ret = "成功";
        if (msg != null && msg.contains("成功")) {
        } else {
            throw new Exception(msg);
        }
        return msg;
    }

    //取消一个订单的发运
    public static String DELIVERY_06(String deliveryId, DeliveryOrderBean deliveryOrderBean) throws Exception {
//        return "成功";
        String bizId= deliveryId+","+deliveryOrderBean.orderId;
        String arg0 = String.format( "{\"flowNo\":\"DELIVERY_06\",\"scSequence\":\"%s\",\"account\":\"%s\",\"bizId\":\"%s\"}",SysParameter.ScSequence,SysParameter.user,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析jso
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        String ret="成功";
        if (msg!=null&&msg.contains("成功"))
        {
        }
        else
        {
            throw new Exception(msg);
        }
        return msg;
    }

    //保存货运单
    public static String DELIVERY_07(String deliveryId, LogisticsInfo logisticsInfo) throws Exception {
//        return "成功";
        Gson gson = new Gson();
        String parm =gson.toJson(logisticsInfo);
        String arg0 = String.format( "{\"flowNo\":\"DELIVERY_07\",\"account\":\"%s\",\"jsonData\":%s,\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user,parm, SysParameter.ScSequence,deliveryId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {

        }
        else
        {
            throw new Exception(msg);
        }
        return msg;
    }

    //查询货运单
    public static LogisticsInfo DELIVERY_08(String deliveryId) throws Exception {
//        LogisticsInfo ret = new LogisticsInfo();
//        return ret;
        String arg0 = String.format("{\"flowNo\":\"DELIVERY_08\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}", SysParameter.user, SysParameter.ScSequence, deliveryId);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");

        LogisticsInfo ret = new LogisticsInfo();
        if (msg != null && msg.contains("成功")) {
            JSONObject jsonArray = data.getJSONObject("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(), new TypeToken<LogisticsInfo>() {
            }.getType());
        } else {
            throw new Exception(msg);
        }
        return ret;
    }

    //上传图片 deliveryId 订单 photoData图片数据
    public static String DELIVERY_09(String deliveryId, byte[] photoData) throws Exception {
//        return "成功";
        LogisticsInfo logisticsInfo = new LogisticsInfo();
        logisticsInfo.deliveryId = deliveryId;
        logisticsInfo.photoData = photoData;
        Gson gson = new Gson();
        String parm = gson.toJson(logisticsInfo);
        String arg0 = String.format("{\"flowNo\":\"DELIVERY_09\",\"account\":\"%s\",\"jsonData\":%s,\"scSequence\":\"%s\"}", SysParameter.user, parm, SysParameter.ScSequence);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        String ret = "成功";
        if (msg != null && msg.contains("成功")) {
        } else {
            throw new Exception(msg);
        }
        return ret;
    }

    //查询物流公司
    public static List<LogisticsCompanyBean> DELIVERY_10(String depotId) throws Exception {
//        List<LogisticsCompanyBean> ret = new ArrayList<>();
//        LogisticsCompanyBean dto = new LogisticsCompanyBean();
//        dto.logisticsCompany = "1";
//        dto.logisticsCompanyID = "1";
//        dto.telephone = "23323223232";
//        ret.add(dto);
//        LogisticsCompanyBean dto2 = new LogisticsCompanyBean();
//        dto2.logisticsCompany = "2";
//        dto2.telephone = "2322323";
//        ret.add(dto2);
//        return ret;
//        String bizId = "2021122300000002";
        List<LogisticsCompanyBean> ret = new ArrayList<>();
        String arg0 = String.format( "{\"flowNo\":\"DELIVERY_10\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,depotId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<List<LogisticsCompanyBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //查询发运当照片列表
    public static List<LogisticsPictureUrlBean> DELIVERY_11(String deliveryId) throws Exception {
//        List<LogisticsPictureUrlBean> ret = new ArrayList<>();
//        LogisticsPictureUrlBean l1 = new LogisticsPictureUrlBean();
//        l1.pictureUrl = "https://c-ssl.duitang.com/uploads/blog/201402/10/20140210160713_a8HFF.thumb.700_0.jpeg";
//        l1.pictureId = "1";
//        LogisticsPictureUrlBean l2 = new LogisticsPictureUrlBean();
//        l2.pictureUrl = "https://c-ssl.duitang.com/uploads/item/201410/10/20141010171110_3nHR5.jpeg";
//        l2.pictureId = "2";
//        ret.add(l1);
//        ret.add(l2);
//        return ret;
        List<LogisticsPictureUrlBean> ret = new ArrayList<>();
        String arg0 = String.format( "{\"flowNo\":\"DELIVERY_11\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,deliveryId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<List<LogisticsPictureUrlBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //删除发运单照片 bizId 传照片id
    public static String DELIVERY_12(String bizId) throws Exception {
//        return "成功";
        String arg0 = String.format( "{\"flowNo\":\"DELIVERY_12\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        String ret="成功";
        if (msg!=null&&msg.contains("成功"))
        {

        }else
        {
            ret =msg;
            throw new Exception(msg);
        }
        return ret;
    }

    //查询发运单的订单明细
    public static List<DeliveryOrderDetailBean> DELIVERY_13(String orderId) throws Exception {
//        List<DeliveryOrderDetailBean> ret = new ArrayList<>();
//        DeliveryOrderDetailBean deliveryOrderDetailBean = new DeliveryOrderDetailBean();
//        deliveryOrderDetailBean.orderId = "111111";
//        deliveryOrderDetailBean.orderNo = "PSLS099876832324";
//        deliveryOrderDetailBean.packingNo = "PSLS-239239023233223-232";
//        deliveryOrderDetailBean.partCode = "10018595C";
//        deliveryOrderDetailBean.partName = "前悬置支架托架";
//        deliveryOrderDetailBean.supplierCode = "100143";
//        deliveryOrderDetailBean.supplierName = "湖北***有限公司";
//        deliveryOrderDetailBean.partCount = "10";
//        ret.add(deliveryOrderDetailBean);
//        return ret;
        List<DeliveryOrderDetailBean> ret = new ArrayList<>();
        String arg0 = String.format( "{\"flowNo\":\"DELIVERY_13\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,orderId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<List<DeliveryOrderDetailBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //查询同一个客户正在发送的配送单信息，如果一个客户同时拍的有多个单子，会弹窗让客户选择一个发运单合并发送
    public static  List<CustomerDeliveryBean> DELIVERY_14(String orderId)throws Exception{
//        List<CustomerDeliveryBean> ret = new ArrayList<>();
//        CustomerDeliveryBean customerDeliveryBean = new CustomerDeliveryBean();
//        customerDeliveryBean.deliveryId = "ID***";
//        customerDeliveryBean.deliveryNo = "ID***";
//        ret.add(customerDeliveryBean);
//        return ret;
        List<CustomerDeliveryBean> ret = new ArrayList<>();
        String arg0 = String.format( "{\"flowNo\":\"DELIVERY_14\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,orderId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<List<CustomerDeliveryBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //发运
    public static String DELIVERY_15(String deliveryId) throws Exception {
//        return "成功";
        String arg0 = String.format( "{\"flowNo\":\"DELIVERY_15\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,deliveryId);
        SoapObject soapObject  = CallWebservice(arg0);
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        String ret="成功";
        if (msg!=null&&msg.contains("成功"))
        {

        }else
        {
            ret =msg;
            throw new Exception(msg);
        }
        return ret;
    }

    //将一个订单创建发运单，生成发运单信息
    public static DeliveryBean DELIVERY_16(String orderId)throws Exception {
//        DeliveryBean ret = new DeliveryBean();
//        return ret;
        String arg0 = String.format("{\"flowNo\":\"DELIVERY_16\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}", SysParameter.user, SysParameter.ScSequence, orderId);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");

        DeliveryBean ret = new DeliveryBean();
        if (msg != null && msg.contains("成功")) {
            JSONObject jsonArray = data.getJSONObject("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(), new TypeToken<DeliveryBean>() {
            }.getType());
        } else {
            throw new Exception(msg);
        }
        return ret;
    }

    //取消一个发运单
    public static String DELIVERY_17(String deliveryId)throws Exception {
//        return "成功";
        String arg0 = String.format( "{\"flowNo\":\"DELIVERY_17\",\"scSequence\":\"%s\",\"account\":\"%s\",\"bizId\":\"%s\"}",SysParameter.ScSequence,SysParameter.user,deliveryId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析jso
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        String ret="成功";
        if (msg!=null&&msg.contains("成功"))
        {
        }
        else
        {
            throw new Exception(msg);
        }
        return msg;
    }

    public static SoapObject CallWebservice(String arg0) throws Exception {
        //实例化SoapObject对象：命名空间+方法名称
        SoapObject request = new SoapObject(WS_Namespace, WS_Method);
        //设置WebService接口需要传入的参数
        request.addProperty("arg0", arg0);

        //创建SoapSerializationEnvelope 对象，同时指定soap版本号
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
        envelope.bodyOut = request;//由于是发送请求，所以是设置bodyOut
        envelope.dotNet = false;//由于是.net开发的webservice，所以这里要设置为true

        //调用接口
        ArrayList<HeaderProperty> headerPropertyArrayList = new ArrayList<HeaderProperty>();
        headerPropertyArrayList.add(new HeaderProperty("Connection", "close"));
        HttpTransportSE httpTransportSE = new HttpTransportSE(GetWS_Url());
        httpTransportSE.call(null, envelope, headerPropertyArrayList);//调用

        // 获取返回的数据
        SoapObject result = (SoapObject) envelope.bodyIn;
        return result;
    }
}
